package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.FixedCodingMapper;
import com.gxa.zhonglong.mapper.GoodsMapper;
import com.gxa.zhonglong.pojo.FixedCoding;
import com.gxa.zhonglong.pojo.Goods;
import com.gxa.zhonglong.pojo.WarehouseWarrant;
import com.gxa.zhonglong.pojo.Where;
import com.gxa.zhonglong.service.FixedCodingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;

@Service
public class FixedCodingServiceImp implements FixedCodingService {

    @Autowired
    FixedCodingMapper fixedCodingMapper = null;

    @Autowired
    GoodsMapper goodsMapper = null;

    public void timeToStand(List<Map<String,Object>> list){
        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);
            System.out.println(map);
            Set set = ((HashMap) map).keySet();
            Iterator iterator = set.iterator();
            while (iterator.hasNext()) {
                String key = iterator.next().toString();
                Object o = ((HashMap) map).get(key);
                if (o instanceof Timestamp) {
                    ((HashMap) map).put(key,o.toString());
                }
            }
        }
    }

    @Override
    public void insertFixedCoding(FixedCoding fixedCoding) {
        fixedCodingMapper.insertFixedCoding(fixedCoding);
    }

    @Override
    public void insertFixedCodingGD(Goods goods) {
        fixedCodingMapper.insertFixedCodingGD(goods);
    }

    @Override
    public List<Map<String, Object>> selectByReceiptNumberFixedCoding(String receiptNumber) {
        List<Map<String, Object>> maps = fixedCodingMapper.selectByReceiptNumberFixedCoding(receiptNumber);
        return maps;
    }

   /* @Override
    public List<WarehouseWarrant> selectByWhere(Where where) {
        Integer page = where.getPage();
        Integer size = where.getSize();
        where.setSize((page-1)*size);
        List<WarehouseWarrant> maps = fixedCodingMapper.selectByWhere(where);
        return maps;
    }*/

    @Override
    public List<Map<String,Object>> selectByWhere(Where where) {
        Integer page = where.getPage();
        Integer size = where.getSize();
        where.setPage((page-1)*size);
        List<Map<String, Object>> selectByWhere = fixedCodingMapper.selectByWhere(where);
        timeToStand(selectByWhere);
        return selectByWhere;
    }

  /*  @Override
    public List<WarehouseWarrant> selectByWhereNoPage(Where where) {
        where.setPage(null);
        List<WarehouseWarrant> maps = fixedCodingMapper.selectByWhere(where);
        return maps;
    }*/
  @Override
  public List<Map<String,Object>> selectByWhereNoPage(Where where) {
      where.setPage(null);
      List<Map<String, Object>> selectByWhere = fixedCodingMapper.selectByWhere(where);
      return selectByWhere;
  }


    @Override
    public void updateByReceiptNumber(FixedCoding fixedCoding) {
        fixedCodingMapper.updateByReceiptNumber(fixedCoding);
    }

    @Override
    public List<Map<String, Object>> selectByReceiptNumberGoods(String receiptNumber) {
        List<Map<String, Object>> maps = fixedCodingMapper.selectByReceiptNumberGoods(receiptNumber);
        return maps;
    }

    @Override
    public void deleteByReceiptNumberFixedCoding(String receiptNumber) {
        fixedCodingMapper.deleteByReceiptNumberFixedCoding(receiptNumber);
    }


    @Override
    public void deleteByNumber(String number) throws BusinessException {
        List<Map<String, Goods>> maps = goodsMapper.selectByNumber(number);
        if (maps.size()>0){
            fixedCodingMapper.deleteByNumber(number);
        }else {
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }

    }
}
